package com.lge.pitchingcoins.util.lockkey;

import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import java.lang.reflect.Field;

public class WindowManagerForLessThanKK implements IWindowManagerLayoutParams {
    private Field extendFields = null;
    private LayoutParams layoutParams = null;
    
    public WindowManagerForLessThanKK(LayoutParams layoutParams) {
        try {
            extendFields = WindowManager.LayoutParams.class.getDeclaredField("extend");
            if (extendFields != null) {
                extendFields.setAccessible(true);
            }
        } catch (Exception e) {
            extendFields = null;
        }
        
        this.layoutParams = layoutParams;
    }
    
    @Override
    public void addLayoutParamExtendFlag(int flag) {
        if (extendFields != null && layoutParams != null) {
            try {
                int extend = (int)extendFields.getInt(layoutParams);
                extend |= flag;
                extendFields.setInt(layoutParams, extend);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setLayoutParamExtendFlag(int flag) {
        if (extendFields != null && layoutParams != null) {
            try {
                extendFields.setInt(layoutParams, flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int getLayoutParamExtendFlag() {
        if (extendFields != null && layoutParams != null) {
            try {
                return extendFields.getInt(layoutParams);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return 0;
    }

    @Override
    public LayoutParams getAttributes() {
        return layoutParams;
    }

    @Override
    public boolean isExtendFieldAccessible() {
        if (extendFields != null) {
            return true;
        }
        
        return false;
    }
}